package com.eCommerce.server.dao.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.hibernate.FetchMode;
import org.hibernate.HibernateException;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.eCommerce.corecontract.criteria.LigneCommandeCriteria;
import com.eCommerce.corecontract.dto.CategorieDTO;
import com.eCommerce.corecontract.dto.ComposantDTO;
import com.eCommerce.corecontract.dto.LigneCommandeDTO;
import com.eCommerce.corecontract.exception.EcommerceException;
import com.eCommerce.corecontract.exception.EcommercePersisenceException;
import com.eCommerce.server.corebackend.dao.AbstractCommonHibernateDAO;
import com.eCommerce.server.corebackend.dao.ILigneCommandeDAO;

public class LigneCommandeDAOImpl extends AbstractCommonHibernateDAO implements ILigneCommandeDAO{
	private static Logger log =Logger.getLogger(LigneCommandeDAOImpl.class.getName());

	public void delete(LigneCommandeDTO lignecommandeDTO)
			throws EcommerceException {
		// TODO Auto-generated method stub
		try {
			getHibernateTemplate().delete(lignecommandeDTO);
		} catch (DataAccessException e) {
			Object[] arg=new Object[1];
			EcommercePersisenceException persistenceException=new EcommercePersisenceException(messagesXmlFileName,"ECOMMERCEDAOLIGNECOMMANDE-005",arg,e.getMessage());
			log.error(persistenceException.getMessage());
			throw persistenceException;
		}
		
	}


	public List find(
			LigneCommandeCriteria lignecommandeCriteria)
			throws EcommerceException {
		DetachedCriteria criteria=null;
		try {
			 criteria = DetachedCriteria
			.forClass(LigneCommandeDTO.class);
criteria.add(Restrictions.eq("id", lignecommandeCriteria.getId()
			.toString()));
		List listOfLigneCommande=getHibernateTemplate().findByCriteria(criteria);
	
		
		return listOfLigneCommande;
		} catch (DataAccessException e) {
			Object[] arg=new Object[1];
			EcommercePersisenceException persistenceException=new EcommercePersisenceException(messagesXmlFileName,"ECOMMERCEDAOLIGNECOMMANDE-004",arg,e.getMessage());
			log.error(persistenceException.getMessage());
			throw persistenceException;
		}
		
		
	}

	public List findAll() throws EcommerceException {
		// TODO Auto-generated method stub
		try {
			DetachedCriteria criteria = DetachedCriteria.forClass(LigneCommandeDTO.class);
			
			List listOfLigneCommande=getHibernateTemplate().findByCriteria(criteria);
			
			return listOfLigneCommande;
		} catch (DataAccessException e) {
			Object[] arg=new Object[1];
			EcommercePersisenceException persistenceException=new EcommercePersisenceException(messagesXmlFileName,"ECOMMERCEDAOLIGNECOMMANDE-004",arg,e.getMessage());
			log.error(persistenceException.getMessage());
			throw persistenceException;
		}
		
	
	}

	public LigneCommandeDTO findOne(LigneCommandeDTO lignecommandeDTO)
			throws EcommerceException {
		
		try {
			DetachedCriteria criteria = DetachedCriteria.forClass(LigneCommandeDTO.class);
			criteria.add(Restrictions.eq("id", lignecommandeDTO.getId()));
			criteria.setFetchMode("composants", FetchMode.JOIN);
			List listOfLigneCommande=getHibernateTemplate().findByCriteria(criteria);
			lignecommandeDTO = (LigneCommandeDTO) listOfLigneCommande.get(0);
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DataAccessException e) {
			Object[] arg=new Object[1];
			EcommercePersisenceException persistenceException=new EcommercePersisenceException(messagesXmlFileName,"ECOMMERCEDAOLIGNECOMMANDE-003",arg,e.getMessage());
			log.error(persistenceException.getMessage());
			throw persistenceException;
		}

		return lignecommandeDTO;
	}


	public Long insert(LigneCommandeDTO lignecommandeDTO)
			throws EcommerceException {
		// TODO Auto-generated method stub
		try {
			getHibernateTemplate().save(lignecommandeDTO);
		} catch (DataAccessException e) {
			Object[] arg=new Object[1];
			EcommercePersisenceException persistenceException=new EcommercePersisenceException(messagesXmlFileName,"ECOMMERCEDAOLIGNECOMMANDE-001",arg,e.getMessage());
			log.error(persistenceException.getMessage());
			throw persistenceException;
		}
		return lignecommandeDTO.getId();
	}


	public void update(LigneCommandeDTO lignecommandeDTO)
			throws EcommerceException {
		// TODO Auto-generated method stub
		try {
			getHibernateTemplate().update(lignecommandeDTO);
		} catch (DataAccessException e) {
			Object[] arg=new Object[1];
			EcommercePersisenceException persistenceException=new EcommercePersisenceException(messagesXmlFileName,"ECOMMERCEDAOLIGNECOMMANDE-002",arg,e.getMessage());
			log.error(persistenceException.getMessage());
			throw persistenceException;
		}
		
	}

}
